package edu.cornell.cs4740.qa.parsing;

import static org.junit.Assert.fail;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.store.LockObtainFailedException;
import org.junit.Test;

import edu.cornell.cs4740.qa.Document;
import edu.cornell.cs4740.qa.Sentence;
import edu.cornell.cs4740.qa.WordContainer;
import edu.cornell.cs4740.qa.ir.DocumentQuery;
import edu.cornell.cs4740.qa.pos.PosTagger;

public class TokenFactoryTest {

	@Test
	public void test()  {
		
		Scanner s = null;
		try {
			s = new Scanner(new File("data/testfile.txt"));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			fail("Exception :(");
		}
		String doc = "";
		while(s.hasNext()) {
			doc += s.nextLine();
			doc += " ";
		}

		TokenFactory tokenFactory = new TokenFactory();
		List<String> sentences = tokenFactory.getSentences(doc);
		for(String st : sentences) {
			List<WordContainer> tokenList = tokenFactory.sentenceToTokens(st);
			PosTagger posTagger = new PosTagger(new File("models/pos-en-general-brown.HiddenMarkovModel"));
			posTagger.tagTokens(tokenList);
			
			for(WordContainer wc : tokenList) {
				System.out.println("Word: " + wc.getWord());
				System.out.println("Stem: " + wc.getStemmedWord());
				System.out.println("POS: " + wc.getPos());
				System.out.println("---------");
			}
			System.out.println(st);
			
		}
	}

	@Test
	public void testDocumentSearch() {
		Scanner s = null;
		try {
			s = new Scanner(new File("data/testfile.txt"));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			fail("Exception :(");
		}
		String doc = "";
		while(s.hasNext()) {
			doc += s.nextLine();
			doc += " ";
		}


		Document dummyDoc = new Document("123", "1234", 1, 12.0f);
		
		List<String> docSents = (new TokenFactory()).getSentences(doc);
		
		for(String sent : docSents) {
			//dummyDoc.addSentence(new Sentence());
			dummyDoc.addSentenceString(sent);
		}
		
		dummyDoc.addDocText(doc);
		
		try {
			DocumentQuery dq = new DocumentQuery(dummyDoc, true);
			List<String> searchPhrase = new ArrayList<String>();
			
			searchPhrase.add("locking");
			searchPhrase.add("antenna");
			
			List<String> relSent = dq.getMostRelevantSentences(searchPhrase, 10);
			
			for(String sent : relSent) {
				System.out.println(sent);
			}
		} catch (CorruptIndexException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (LockObtainFailedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	@Test
	@Deprecated
	public void testMultiDocumentSearch() {
		Scanner s = null;
		try {
			s = new Scanner(new File("data/testfile.txt"));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			fail("Exception :(");
		}
		String doc = "";
		while(s.hasNext()) {
			doc += s.nextLine();
			doc += " ";
		}


		Document dummyDoc = new Document("123", "1234", 1, 12.0f);
		
		List<String> docSents = (new TokenFactory()).getSentences(doc);
		
		for(String sent : docSents) {
			//dummyDoc.addSentence(new Sentence());
			dummyDoc.addSentenceString(sent);
		}
		
		dummyDoc.addDocText(doc);
		
		try {
			DocumentQuery dq = new DocumentQuery(dummyDoc, true);
			List<String> searchPhrase = new ArrayList<String>();
			
			searchPhrase.add("locking");
			searchPhrase.add("antenna");
			
			List<String> relSent = dq.getMostRelevantSentences(searchPhrase, 10);
			
			for(String sent : relSent) {
				System.out.println(sent);
			}
		} catch (CorruptIndexException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (LockObtainFailedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
